package patrol.mobile.com.mobilepatrol;

import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.bridge.ScanActivity;
import patrol.mobile.com.mobilepatrol.test.GsonTest;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

/**
 * Created by apple on 17/10/11.
 */

public class ScanTest extends BaseActivity implements QRCodeView.Delegate{
    private static final String TAG = ScanActivity.class.getSimpleName();
    public QRCodeView mQRCodeView;

    @Override
    protected void onStart() {
        super.onStart();
        // 打开后置摄像头开始预览，但是并未开始识别
        mQRCodeView.startCamera();
        //显示扫描框
        mQRCodeView.showScanRect();
        //开始扫描
        mQRCodeView.startSpot();
    }

    @Override
    protected void onStop() {
        //关闭摄像头预览，并且隐藏扫描框
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }


    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        mQRCodeView.stopSpot();

       startActivity(GsonTest.class);
        //开始扫描
        mQRCodeView.startSpot();

    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(ScanTest.this,"打开相机出错", Toast.LENGTH_SHORT).show();
        Log.e(TAG, "打开相机出错");
        finish();
    }
    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(ScanTest.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("扫描二维码");
    }

    @Override
    public void initView() {
        super.initView();
        mQRCodeView = (ZXingView) findViewById(R.id.zxingview);
        //设置扫描二维码的代理
        mQRCodeView.setDelegate(this);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_scan;
    }

    @Override
    public void setListener() {

    }
    //震动
    private void vibrate() {
        Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }
}
